import datetime
import unittest
from core import convert, equals, CurrencyConvertError
from codes import *

class TestConvert(unittest.TestCase):
    def testEquals(self):
        self.assertTrue(equals(1.00, 1))
        self.assertTrue(equals(1.001, 1))
        self.assertTrue(equals(0.331, 0.33))
        self.assertTrue(equals(0.3333333, 0.33))
        self.assertTrue(equals(0.334, 0.33))
        self.assertTrue(equals(0.339, 0.34))

    def test_rub_rur(self):
        self.assertEquals(convert(1, RUR, RUR), 1.0)
        self.assertEquals(convert(10000.0, RUR, RUB), 10.0)

    def test_known_date(self):
        v = convert(1, USD, RUB, datetime.date(2000, 10, 16))
        self.assertTrue(equals(v, 27.90))
        v = convert(1, UAH, RUB, datetime.date(2000, 10, 15))
        self.assertTrue(equals(v, 5.10))

    def test_current_date(self):
        v = convert(1, USD, RUB)
        self.assertTrue(v > 0)

    def test_1995_date(self):
        v = convert(1, USD, RUB, datetime.date(1995, 10, 14))
        #self.assertTrue(equals(v, 5.10))

    def testErrorCurrency(self):
        self.assertRaises(CurrencyConvertError,
            lambda: convert(1, 1234567, RUB, datetime.date(2000, 10, 10)))

#    def testErrorDate(self):
#        try:
#            convert(1, EUR, RUB, datetime.date(1983, 10, 10))
#        except CurrencyConvertError:
#            self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()
